/**
 * Created by Administrator on 2018/8/17.
 */
$CertusJsonAjax.ajax("queryPurchaseComparisionProcessTrace",{
    "processInstanceId": $CertusJsonAjax.getProInstanceId(),
    "stepInstanceId": $CertusJsonAjax.getStepInstanceId()
},function(res){
    $$scope.processCCBXTrace = res.data;
})
// 资产库可用资产信息
var purchaseProductDatas = new Array();
// 采购记录
var proc1Columns = [
    {
        id: 'commodityCateName',
        title: '类目',
        width: 73,
        type: 'string',
        columnClass: 'text-center',
    }, {
        id: 'commodityClassName',
        title: '类别',
        width: 73,
        type: 'string',
        columnClass: 'text-center',
    }, {
        id: 'name',
        title: '名称',
        width: 73,
        type: 'string',
        columnClass: 'text-center',
    }, {
        id: 'descAndSpec',
        title: '品名及规格',
        width: 200,
        type: 'string',
        columnClass: 'text-left',
    }, {
        id: 'quantity',
        title: '数量',
        width: 100,
        type: 'string',
        columnClass: 'text-center',
    }, {
        id: 'estimatedAmount',
        title: '预计金额',
        width: 150,
        type: 'string',
        columnClass: 'text-center',
        resolution: function (value, record, column, grid, dataNo, columnNo) {
            var content = '￥' + value + '元';
            return content;
          }
    }, {
        id: 'remark',
        title: '备注',
        type: 'string',
        columnClass: 'text-left',
    }, {
        id: 'operation',
        title: '操作',
        width: 120,
        type: 'string',
        columnClass: 'text-center',
        resolution: function (value, record, column, grid, dataNo, columnNo) {
            var content = '';
            content += '<button class="btn btn-xs btn-success" onclick="showNowQuote(\'' + record.biCode + '\')"><i class="icon-eye-open"></i>  查看</button>';
            content += '  ';
            return content;
        }
    }];
var purchaseComparisionStepEntity = {
    "processInstanceId": $CertusJsonAjax.getProInstanceId(),
    "stepInstanceId": $CertusJsonAjax.getStepInstanceId(),
}
var bqArray = {};
var oldBqArray = {};
// 采购比价单信息
var pryslColumns = [
    {
        id: 'name',
        title: '采购物资名称',
        type: 'string',
        width: 150,
        columnClass: 'text-center',
    }, {
        id: 'specification',
        title: '规格和参数',
        type: 'string',
        // width: 150,
        columnClass: 'text-left',
    }, {
        id: 'priceIncludingTax',
        title: '单价',
        type: 'string',
        // width: 150,
        columnClass: 'text-center',
        resolution: function (value, record, column, grid, dataNo, columnNo) {
            var content = '￥' + value + '元';
            return content;
          }
    }, {
        id: 'quantity',
        title: '数量',
        type: 'string',
        // width: 100,
        columnClass: 'text-center',
    }, {
        id: 'totalPrice',
        title: '总价(含税&折扣)',
        type: 'string',
        // width: 150,
        columnClass: 'text-center',
        resolution: function (value, record, column, grid, dataNo, columnNo) {
            var content = '￥' + value + '元';
            return content;
          }
    }, {
        id: 'supplierName',
        title: '供应商名称',
        type: 'string',
        width: 150,
        columnClass: 'text-center',
    }, {
        id: 'supplierPhone',
        title: '联系方式',
        width: 110,
        columnClass: 'text-center',
    }, {
        id: 'address',
        title: '地点',
        type: 'string',
        hideType: 'lg|md|sm|xs',
    }, {
        id: 'price',
        title: '备注说明',
        type: 'string',
        hideType: 'lg|md|sm|xs',
    }, {
        id: 'dateOfDelivery',
        title: '交货期',
        type: 'string',
        hideType: 'lg|md|sm|xs',
        // resolution: function (value, record, column, grid, dataNo, columnNo) {
        //     var date = new Date(record.dateOfDelivery);
        //     var content = $.fn.dlshouwen.grid.tools.dateFormat(date, "yyyy-MM-dd");
        //     return content;
        // }

    }, {
        id: 'guaranteeTime',
        title: '质保期',
        type: 'string',
        hideType: 'lg|md|sm|xs',
        // resolution: function (value, record, column, grid, dataNo, columnNo) {
        //     var date = new Date(record.guaranteeTime);
        //     var content = $.fn.dlshouwen.grid.tools.dateFormat(date, "yyyy-MM-dd");
        //     return content;
        // }
    }, {
        id: 'paymentMethod',
        title: '付款方式',
        type: 'string',
        hideType: 'lg|md|sm|xs',
    }, {
        id: 'strengthsAndWeaknesses',
        title: '优劣势',
        type: 'string',
        hideType: 'lg|md|sm|xs',
    },
    {
        id: 'operation',
        title: '操作',
        width: 50,
        type: 'string',
        columnClass: 'text-center',
        resolution: function (value, record, column, grid, dataNo, columnNo) {
            var content = '';
            /*content += '<button class="btn btn-xs btn-info" onclick="shpriceoxBidCode(\'' + record.bqCode + '\',\'' + record.biCode + '\')"><i class="fa fa-edit"></i>  编辑</button>';
            content += '  ';*/
            content += '<button class="btn btn-xs btn-primary" onclick="openDownloadList(\'' + record.bqCode + '\',\'' + record.biCode + '\')"><i class="icon-eye-open"></i>  附件</button>';
            content += '  ';
           /* content += '<button class="btn btn-xs btn-danger" onclick="del(\'' + record.bqCode + '\',\'' + record.biCode + '\');"><i class="fa fa-trash-o"></i>  删除</button>';*/
            return content;
        }
    }];
var belowGrid;
var aboveGrid;
var brCode;

$$scope.wkStepName = $CertusJsonAjax.getWkStepName();

//打开 下载 列表
function openDownloadList(bqCode, biCode) {

    if (biCode && bqCode) {
        var nowBq = bqArray[biCode];
        if (nowBq) {
            for (var i in nowBq) {
                if (nowBq[i].supplierFile && nowBq[i].bqCode == bqCode) {
                    $$scope.modalContent = eval(nowBq[i].supplierFile);
                }
            }

        }
    }
    // $$scope.modalContent = data.data;
    $$scope.ok = function () {
        $modalInstance.close(); // 点击确认后关闭弹窗
    };
    $$scope.cancel = function () {
        $modalInstance.dismiss('cancel'); // 点击取消后撤销模态框
    }
    var modalContent = $$scope.modalContent;

    $fileListService.openConfirmWindow("提示", modalContent).then(function (res) {
    });
}

$$scope.getsssbbb = function(bb){
    ;
    //starting denied sentback others completed
    if(bb == "starting"){
        return "正在进行中";
    }else if(bb == "denied"){
        return "否决";
    }else if(bb == "sentback"){
        return "被退";
    }else if(bb == "others"){
        return "关闭";
    }else if(bb == "completed"){
         return "完成";    }else if(bb == "terminated"){        return "终结";    }    else if(bb == "cancelled"){        return "通过";}
    return "待办";
}

$$scope.comparisionApprove = function(){

    if (certusValidateNotnull()) {
        $CertusJsonAjax.ajax("comparisionApprove", $$scope.submmitData.purchaseComparisionStepEntity, function (res) {
            if (res.data) {
                if (res.data == "success") {
                    findBacklogPerson($CertusFormAjax,$CertusJsonAjax.getStepInstanceId());
                    window.history.back();
                    return;
                }
            }
            layer.msg(res.data);
        })
    }
}

$CertusJsonAjax.ajax("comparisionExamineView", {
    "processInstanceId": $CertusJsonAjax.getProInstanceId(),
    "stepInstanceId": $CertusJsonAjax.getStepInstanceId()
}, function (res) {
    if (res.data) {
        // $$scope.headerMap = res.data.headerMap;
        $$scope.submmitData = {};
        $$scope.submmitData.purchaseComparisionStepEntity = {
            "processInstanceId": $CertusJsonAjax.getProInstanceId(),
            "stepInstanceId": $CertusJsonAjax.getStepInstanceId()
        };
        $$scope.submmitData.purchaseComparisionStepEntity.buyingRequisitionCode = res.data.headerMap.buyingRequisitionCode;
        $$scope.submmitData.purchaseComparisionStepEntity.purchasingMsgDis = res.data.headerMap.purchasingMsg;

        var datas = res.data.buyings;
        $$scope.processs = res.data.processs;
        var priceOption1 = {
            lang: 'zh-cn',
            ajaxLoad: false,
            extraWidth: 20,
            exportFileName: '采购单条目信息',
            datas: datas,
            columns: proc1Columns,
            gridContainer: 'plistogContainer'
        };
        aboveGrid = $.fn.dlshouwen.grid.init(priceOption1);
        aboveGrid.load();

        //初始化 报价 文件 数据
        if (datas) {
            for (var i in datas) {
                if (datas[i].biCode) {
                    bqArray[datas[i].biCode] = datas[i].buyingQuoteItemEntities;
                }
            }
            //初始化 历史报价 文件 数据
            for (var i in datas) {
                if (datas[i].biCode) {
                    oldBqArray[datas[i].biCode] = datas[i].oldBuyingQuoteItemEntities;
                }
            }
        }

        var tempArray = new Array();
        if (bqArray) {
            for (var j in bqArray) {
                tempArray = tempArray.concat(bqArray[j]);
            }
        }
        var prcListOption = {
            lang: 'zh-cn',
            ajaxLoad: false,
            extraWidth: 20,
            // loadURL:'url',
            exportFileName: '资产信息',
            datas: tempArray,
            columns: pryslColumns,
            gridContainer: 'pricelogContainer',
            tools: '',
            pageSize: 10,
            pageSizeLimit: [10, 15, 20]
        };
        belowGrid = $.fn.dlshouwen.grid.init(prcListOption);
        belowGrid.load();
        $(".hdgbuy").show();
    }
});

$$scope.submitter = {};
$$scope.select = {};
$$scope.select.itAcceptanceSelect = [
    {name: "合格", code: "1"}, {name: "不合格", code: "0"}
]
$$scope.select.acceptanceResolveSelect = [
    {name: "合格", code: "1"}, {name: "不合格", code: "0"}
]






function showNowQuote(biCode) {
    if (biCode) {
        belowGrid.exhibitDatas = bqArray[biCode];
        belowGrid.refresh(false);
        $(".hdlog").show();
        $(".hdgbuy").show();
    }
}

function showAllQuote() {
    var tempArray = new Array();
    if (bqArray) {
        for (var j in bqArray) {
            tempArray = tempArray.concat(bqArray[j]);
        }
    }
    belowGrid.exhibitDatas = tempArray;
    belowGrid.refresh(false);

    $(".hdgbuy").show();
}




function openAcceptance(bqCode, biCode) {

    if (bqCode && biCode) {
        $$scope.sbsb = {}
        $$scope.sbsb.bqCode = bqCode
        $$scope.sbsb.biCode = biCode;
        var bqArray2 = bqArray[biCode];
        if (bqArray2) {
            for (var i in bqArray2) {
                $$scope.acceptance = {}
                if (bqArray2[i].bqCode == bqCode) {
                    if(bqArray2[i].recievedNum){
                        $$scope.acceptance.recievedNum = bqArray2[i].recievedNum;
                    }
                    if(bqArray2[i].unit){
                        $$scope.acceptance.unit = bqArray2[i].unit
                    }
                    if(bqArray2[i].acceptanceResolve){
                        $$scope.acceptance.acceptanceResolve = bqArray2[i].acceptanceResolve
                    }
                    if( bqArray2[i].itAcceptance){
                        $$scope.acceptance.itAcceptance = bqArray2[i].itAcceptance+""
                    }
                    $$scope.$apply();
                }
            }

        }
    }
    $("#myModal").modal('show');
}


$$scope.admitAcceptance = function (bqCode, biCode) {

    if(certusValidateNotnullPolyfill()){
        if (bqCode && biCode) {
            $$scope.sbsb.bqCode = bqCode
            $$scope.sbsb.biCode = biCode;
            var bqArray2 = bqArray[biCode];
            if (bqArray2) {
                for (var i in bqArray2) {
                    if (bqArray2[i].bqCode == bqCode) {
                        bqArray2[i].recievedNum = $$scope.acceptance.recievedNum
                        bqArray2[i].unit = $$scope.acceptance.unit
                        bqArray2[i].acceptanceResolve = $$scope.acceptance.acceptanceResolve
                        bqArray2[i].itAcceptance = $$scope.acceptance.itAcceptance
                    }
                }

            }
            belowGrid.exhibitDatas = bqArray[biCode];
            belowGrid.refresh(false);
        }
        $("#myModal").modal('hide');
    }
}


function shupload(bqCode, biCode) {
    $(".hdupload").show();
    $$scope.agreementAaaa = {};
    $$scope.agreementAaaa.bqCode = bqCode;
    $$scope.agreementAaaa.biCode = biCode;
}

function openAgreementDownloadList(bqCode, biCode) {

    if (biCode && bqCode) {
        var nowBq = bqArray[biCode];
        if (nowBq) {
            for (var i in nowBq) {
                if (nowBq[i].supplierFile && nowBq[i].bqCode == bqCode) {
                    $$scope.modalContent = eval(nowBq[i].agreementFile)
                }
            }
        }
    }
    // $$scope.modalContent = data.data;
    $$scope.ok = function () {
        $modalInstance.close(); // 点击确认后关闭弹窗
    };
    $$scope.cancel = function () {
        $modalInstance.dismiss('cancel'); // 点击取消后撤销模态框
    }
    var modalContent = $$scope.modalContent;

    $fileListService.openConfirmWindow("提示", modalContent).then(function (res) {
    });
}


function hdgs() {
    $(".hdgs").hide();
}

//打开 打开 历史库下载列表
function downOldAgreement(bqCode, biCode) {
    if (biCode && bqCode) {
        var nowBq = oldBqArray[biCode];
        if (nowBq) {
            for (var i in nowBq) {
                if (nowBq[i].supplierFile && nowBq[i].bqCode == bqCode) {
                    $$scope.modalContent = eval(nowBq[i].agreementFile)
                }
            }
        }
    }
    // $$scope.modalContent = data.data;
    $$scope.ok = function () {
        $modalInstance.close(); // 点击确认后关闭弹窗
    };
    $$scope.cancel = function () {
        $modalInstance.dismiss('cancel'); // 点击取消后撤销模态框
    }
    var modalContent = $$scope.modalContent;

    $fileListService.openConfirmWindow("提示", modalContent).then(function (res) {
    });
}




$$scope.approvalAgreementSubmit = function () {

    if (certusValidateNotnull()) {
        $$scope.submmitData.purchaseStepEntity.findWhoType = 'C';
        $$scope.submmitData.buyingQuoteItemEntities = bqArray;
        $CertusJsonAjax.ajax("confirmPrintPurchase", $$scope.submmitData, function (res) {
            if (res.data) {
                if (res.data == "success") {
                    findBacklogPerson($CertusFormAjax,$CertusJsonAjax.getStepInstanceId());
                    window.history.back();
                    return;
                }
                layer.msg(res.data);
            }
        })
    }
}

//文件列表下载
function downLoad(id) {
    var fileID = $(id).parent().parent().find(".fileDownloadPath").val();
    $CertusFormAjax.download("downloadFile", "fileId=" + fileID);
}
